package services;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.jws.WebService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import model.User;
import model.fuji.wb.CANCELBARCODECERTIFY;
import model.fuji.wb.CANCELBARCODEINFO;
import model.fuji.wb.CANCELTRADEINFO;
import model.fuji.wb.CXPAYREQUESTINFO;
import model.fuji.wb.CZKINFO;
import model.fuji.wb.FINDGOODSTIFY;
import model.fuji.wb.FINISHINFO;
import model.fuji.wb.GOODSINFO;
import model.fuji.wb.PAYINFO;
import model.fuji.wb.POSINFO;
import model.fuji.wb.TRADEINFO;
import model.fuji.wb.VIPCERTIFY;
import model.fuji.wb.VIPINFO;

@WebService(endpointInterface = "services.IRemoteService")
public class RemoteServiceImpl {
	public String showRemoteMessage(String message) {
		return "You message is " + message;
	}

	public String addOrder(List<User> users) {
		return JSONArray.toJSONString(users, true);
	}

	static String dataFormater(Object o) {
		return o.getClass().getSimpleName() + "$" + JSON.toJSONString(o);
	}

	static List<GOODSINFO> allGoods = new ArrayList<GOODSINFO>();
	static Integer flow_no = 1;

	String getRandomString(int n) {
		// 定义一个字符串（A-Z，a-z，0-9）即62位；
		String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
		// 由Random生成随机数
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		// 长度为几就循环几次
		for (int i = 0; i < n; ++i) {
			// 产生0-61的数字
			int number = random.nextInt(62);
			// 将产生的数字通过length次承载到sb中
			sb.append(str.charAt(number));
		}
		// 将承载的字符转换成字符串
		return sb.toString();
	}

	public String callMethod(String inStr) {
		String info[] = inStr.split("\\$");
		System.out.println(inStr);
		if (info.length < 2)
			return "Error${\"retflag\":=\"1\",\"retmsg\":\"request data is wrong!\"}";
		// 开始
		if(info[0].equals("POSCERTIFY")) {
			POSINFO resp = new POSINFO();
			resp.setFlow_no(flow_no.toString());
			resp.setRetflag("0");
			resp.setRetmsg("success");
			return dataFormater(resp);
		}
		// 会员登陆
		else if(info[0].equals("VIPCERTIFY")) {
			VIPCERTIFY request = JSON.parseObject(info[1], VIPCERTIFY.class);
			VIPINFO vipInfo = new VIPINFO();
			vipInfo.setRetflag("0");
			vipInfo.setRetmsg("success");
			vipInfo.setVipno(request.getVipno());
			return dataFormater(vipInfo);
		}
		// 扫描商品
		else if(info[0].equals("FINDGOODSTIFY")) {
			FINDGOODSTIFY request = JSON.parseObject(info[1], FINDGOODSTIFY.class);
			GOODSINFO goods = new GOODSINFO();
			String fi = allGoods.size() > 0 ? allGoods.get(allGoods.size() - 1).getFlow_id() : "1";
			goods.setFlow_id(String.valueOf(Integer.valueOf(fi) + 1));
			goods.setDisc(0.05f);
			goods.setDsctotal(0.01f);
			goods.setFname(getRandomString(20));
			goods.setIncode("123");
			goods.setBarcode(request.getBarcode());
			goods.setGoodsno(request.getBarcode());
			goods.setSpecs("p");
			goods.setUnits("kg");
			goods.setPrice(6.18f);
			goods.setUnitprice(3.14f);
			
			goods.setQty(2f);
			goods.setRetflag("0");
			goods.setRetmsg("success");
			goods.setTotal(6.28f);
			allGoods.add(goods);
			return dataFormater(goods);
		}
		// 获取整单
		else if(info[0].equals("TRADECERTIFY")) {
			TRADEINFO whole = new TRADEINFO();
			whole.setRetflag("0");
			whole.setRetmsg("success");
			whole.setList(allGoods);
			return dataFormater(whole);
		}
		// 取消商品
		else if(info[0].equals("CANCELBARCODECERTIFY")) {
			CANCELBARCODECERTIFY request = JSON.parseObject(info[1], CANCELBARCODECERTIFY.class);
			CANCELBARCODEINFO response = new CANCELBARCODEINFO();
			response.setBarcode(request.getBarcode());
			response.setRetflag("0");
			for (int i = 0; i < allGoods.size(); i++) {
				if (request.getFlow_id() == allGoods.get(i).getFlow_id()) {
					allGoods.remove(i);
					break;
				}
			}
			return dataFormater(response);
		}
		// 计算促销
		else if(info[0].equals("CXPAYREQUESTCERTIFY")) {
			BigDecimal sum = new BigDecimal(0), dsctotal = new BigDecimal(0), qty = new BigDecimal(0);

			for (GOODSINFO i : allGoods) {
				sum = sum.add(new BigDecimal(i.getTotal()));
				dsctotal = dsctotal.add(new BigDecimal(i.getDsctotal()));
				qty = qty.add(new BigDecimal(i.getQty()));
			}
			CXPAYREQUESTINFO response = new CXPAYREQUESTINFO();
			response.setSjtotal(Float.valueOf(sum.toString()));
			response.setRetflag("0");
			response.setRetmsg("success");
			response.setFlow_no(flow_no.toString());
			response.setTotal(Float.parseFloat(sum.toString()));
			response.setTotaldsc(Float.parseFloat(dsctotal.toString()));
			response.setQty(Float.parseFloat(qty.toString()));

			return dataFormater(response);
		}
		// 取消订单
		else if(info[0].equals("CANCELTRADECERTIFY")) {
			allGoods.clear();
			CANCELTRADEINFO response = new CANCELTRADEINFO();
			response.setRetflag("0");
			response.setRetmsg("success");

			return dataFormater(response);
		}
		// 支付提交
		else if(info[0].equals("PAYCERTIFY")) {
			PAYINFO reponse = new PAYINFO();
			reponse.setRetflag("0");
			reponse.setRetmsg("success");
			return dataFormater(reponse);
		}
		// 支付完成
		else if(info[0].equals("FINISHCERTIFY")) {
			allGoods.clear();
			flow_no++;
			FINISHINFO finish = new FINISHINFO();
			finish.setRetflag("0");
			finish.setRetmsg("success");
			return dataFormater(finish);
		}
		// 储值卡交易
		else if(info[0].equals("CZKCERTIFY")) {
			CZKINFO czk = new CZKINFO();
			czk.setRetflag("0");
			czk.setRetmsg("success");
			czk.setCardno("888801");
			return dataFormater(czk);
		} else
			return "Error:\"{\"retflag\":\"-1\"}";
	}
}
